package com.market.cash.ui.presenter

import com.market.cash.configs.CashRootConfigs
import com.market.cash.http.ApiService
import com.market.cash.http.IHttpCallback
import com.market.cash.pro.ProPresenter
import com.market.cash.ui.view.ICashRootStepsView
import com.market.cash.utils.helper.CashRootProcessHelper
import com.market.cash.utils.language.LocaleContract
import com.market.cash.utils.language.MultiLanguages
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/21 11:59
 * @Annotation：进件 Presenter 实现
 */
class CashRootStepsPresenter(private val iView: ICashRootStepsView): ProPresenter(iView) {

    /**
     * 当前步骤的名称 _
     */
    private var eventStepTitle = ""
    fun getCashRootEventStepTitle(): String{
        return eventStepTitle
    }

    /**
     * 根据Step类型显示不同的Page
     */
    fun loadCashRootStepPageAction(){
        //当前进行的步骤
        val curtIndex = CashRootProcessHelper.getCashRootStepIndex()
        val allSteps = CashRootProcessHelper.getCashRootAllStepList()
        if (curtIndex == allSteps.size){//所有步骤已完成
            iView.showCashRootStepsComplete()
            return
        }
        val processData = CashRootProcessHelper.getCashRootProcessData()
        eventStepTitle = allSteps[curtIndex]
        val allStepTitles = CashRootProcessHelper.getCashRootStepTitleList()
        var stepTitle = allStepTitles[curtIndex]
        //是否是坦桑地区,需要根据语言显示
        if (CashRootConfigs.isTanz()){
            val stepTitleItem = JSONObject(stepTitle)
            val curtLocale = MultiLanguages.getAppLanguage(iView.getAct())
            stepTitle = when(curtLocale.language){
                LocaleContract.getSwahiliLocale().language -> {//坦桑
                    stepTitleItem.optString("sw")
                }
                else -> {
                    stepTitleItem.optString("en")
                }
            }
        }
        iView.showCashRootStepsProgress(stepTitle,allSteps.size,curtIndex+1)
        //获取对应的key
        val curtKey = allSteps[curtIndex]
        //Contact 类型
        if (processData!!.has(curtKey)){
            val stepData = processData.optJSONObject(curtKey)
            stepData?.put("showTitle",stepTitle)
            stepData?.put("stepTitle",eventStepTitle)
            if (stepData != null && stepData.has("isContact") && stepData.optString("isContact") == "YES"){
                iView.showCashRootStepContactView(stepData)
                return
            }
        }

        //todo Ocr证件照样式
        if (curtKey.contains("ocr_info")){
            val stepData = JSONObject()
            stepData.put("showTitle",stepTitle)
            stepData.put("stepTitle",eventStepTitle)
            iView.showCashRootStepOcrView(stepData)
            return
        }

        //todo Selfie自拍照样式
        if (curtKey.contains("sefie_info")){
            val stepData = JSONObject()
            stepData.put("showTitle",stepTitle)
            stepData.put("stepTitle",eventStepTitle)
            iView.showCashRootStepSelfieView(stepData)
            return
        }

        //todo Account账户样式（法语地区绑定账号）
        if (curtKey.contains("account_info") && CashRootConfigs.isFrench()){
            val stepData = JSONObject()
            stepData.put("showTitle",stepTitle)
            stepData.put("stepTitle",eventStepTitle)
            iView.showCashRootStepAccountView(stepData)
            return
        }
        //常规通用样式
        if (processData.has(curtKey)){
            val stepData = processData.optJSONObject(curtKey)
            if (stepData != null) {
                stepData.put("showTitle",stepTitle)
                stepData.put("stepTitle",eventStepTitle)
                iView.showCashRootStepCommonView(stepData)
            }
        }
    }

    /**
     * 提交数据
     */
    fun submitCashRootProcessAction(params: JSONObject){
        iView.showCashRootLoading()
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.CASHROOT_API_COMMON_URL)
        doCashRootPostAction(httpUrl, params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.showCashRootSubmitSuccess()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hideCashRootLoading()
            }
        })
    }
}